package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallBookListMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.domain.LitemallBookList.Column;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class LitemallBookListService {
    @Resource
    private LitemallBookListMapper bookListMapper;
    private Column[] columns = new Column[]{Column.id, Column.title, Column.subtitle, Column.price, Column.picUrl, Column.readCount};

    public List<LitemallBookList> queryList(int type,int offset, int limit) {
        return queryList(type,offset, limit, "add_time", "desc");
    }

    public List<LitemallBookList> queryList(int type,int offset, int limit, String sort, String order) {
        LitemallBookListExample example = new LitemallBookListExample();
        example.or().andTypeEqualTo(type).andDeletedEqualTo(false);
        example.setOrderByClause(sort + " " + order);
        PageHelper.startPage(offset, limit);
        return bookListMapper.selectByExampleSelective(example);
    }

    public int queryTotal() {
        LitemallBookListExample example = new LitemallBookListExample();
        example.or().andDeletedEqualTo(false);
        return (int) bookListMapper.countByExample(example);
    }

    public LitemallBookList findById(Integer id) {
        LitemallBookListExample example = new LitemallBookListExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return bookListMapper.selectOneByExampleWithBLOBs(example);
    }

    public List<LitemallBookList> queryRelatedList(Integer type,Integer id, int offset, int limit) {
        LitemallBookListExample example = new LitemallBookListExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        List<LitemallBookList> bookLists = bookListMapper.selectByExample(example);
        if (bookLists.size() == 0) {
            return queryList(type,offset, limit, "add_time", "desc");
        }
        LitemallBookList bookList = bookLists.get(0);

        example = new LitemallBookListExample();
        example.or().andIdNotEqualTo(bookList.getId()).andDeletedEqualTo(false);
        PageHelper.startPage(offset, limit);
        List<LitemallBookList> relateds = bookListMapper.selectByExampleWithBLOBs(example);
        if (relateds.size() != 0) {
            return relateds;
        }

        return queryList(type,offset, limit, "add_time", "desc");
    }

    public List<LitemallBookList> querySelective(String title, String subtitle, Integer page, Integer limit, String sort, String order) {
        LitemallBookListExample example = new LitemallBookListExample();
        LitemallBookListExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtils.isEmpty(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return bookListMapper.selectByExampleWithBLOBs(example);
    }

    public int updateById(LitemallBookList bookList) {
        bookList.setUpdateTime(LocalDateTime.now());
        LitemallBookListExample example = new LitemallBookListExample();
        example.or().andIdEqualTo(bookList.getId());
        return bookListMapper.updateByExampleSelective(bookList, example);
    }

    public void deleteById(Integer id) {
        bookListMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallBookList bookList) {
        bookList.setAddTime(LocalDateTime.now());
        bookList.setUpdateTime(LocalDateTime.now());
        bookListMapper.insertSelective(bookList);
    }


    public void deleteByIds(Integer[] ids) {
        LitemallBookListExample example = new LitemallBookListExample();
        example.or().andIdIn(Arrays.asList(ids.clone())).andDeletedEqualTo(false);
        bookListMapper.deleteByExample(example);
    }
}
